/**
 * All Rights Reserved , Copyright (C) 2021 , 青岛鼎信通讯股份有限公司
 *
 *CpCustomerCompanyController
 * 企业客户
 *
 * 修改纪录
 * 2021-06-23 版本：1.0 jiangtao 创建。
 * @version 版本：1.0
 * @author 作者：jiangtao
 * 创建日期：2021-06-23
 */

package com.topscomm.cp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.topscomm.cp.CpController;
import com.topscomm.cp.pojo.CpCustomerCompanyEntity;
import com.topscomm.cp.service.ICpCustomerCompanyService;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.tap.ITapService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.ResponseResult;

/**
 * @author: jiangtao
 * @date: 2021-06-23
 * @description:企业客户Controller
 * @modify:
 */
@RestController
@RequestMapping("cp/customerCompany")
public class CpCustomerCompanyController extends CpController<CpCustomerCompanyEntity, ICpCustomerCompanyService> {
	/**
	 * @author: jiangtao
	 * @date: 2021-06-23
	 * @param paraMap
	 * @return
	 * @description:转换出查询SQL语句
	 * @modify:
	 */
	@Override
	protected String spellListSql(Map<String, Object> paraMap) {
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_customercode")) {
			String value = paraMap.get("qry_customercode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		return sql.toString();
	}
	/**
	  * @description:
	  * @author xinfushuo
	  * @param  paraMap 查询参数
	  * @return	查询拼串
	  * @date 2021/10/22
	  * @modify
	  */
	@Override
	protected String spellRefListSql(Map<String,Object> paraMap){
		StringBufferProxy sql = new StringBufferProxy();
		sql.appendSingle(" 1=1 ");
		if (paraMap.containsKey("qry_name")) {
			String value = paraMap.get("qry_name").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and name like '%{0}%' ", value);
		}
		if (paraMap.containsKey("qry_code")) {
			String value = paraMap.get("qry_code").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		if (paraMap.containsKey("qry_customercode")) {
			String value = paraMap.get("qry_customercode").toString();
			if (!StringUtil.isEmpty(value))
				sql.appendSingle(" and code = '{0}' ", value);
		}
		sql.appendSingle(this.service.appendChooseAuthSql(paraMap,getUserBean()));
		return sql.toString();
	}

	@Override
	@GetMapping({"/refList"})
	public ResponseResult<Object> refList(@RequestParam(required = false) Map<String, Object> paraMap) {
		try {
			paraMap = BaseUtil.decodeSecureMap(paraMap);
			Query query = new Query(paraMap);
			String sqlWhere = this.spellRefListSql(paraMap);
			int totalCount = this.service.getCount(sqlWhere);
			List<Map<String, Object>> tempList = null;
			if (query.getPageSize() > 0) {
				tempList = this.service.queryMapForPage(sqlWhere, query.getCurrentPage(), query.getPageSize(), query.getSidx(), query.getSord());
			} else {
				tempList = this.service.queryMapByWhere(sqlWhere);
			}
			tempList.forEach(item->item.put("customersourcetype",CpCustomerCompanyEntity.tableName));
			Page page = new Page(tempList, totalCount, query.getPageSize(), query.getCurrentPage());
			return ResponseResult.ok(page);
		} catch (Exception var7) {
			return this.handleControllerException(var7);
		}
	}
	@GetMapping({"/BillInfo"})
	public ResponseResult<Object> BillInfo(@RequestParam(required = false) Map<String, Object> paraMap) {
		paraMap = BaseUtil.decodeSecureMap(paraMap);
		Map<String,Object> data = new HashMap<>();
		if (paraMap.containsKey("qry_customercode")) {
			String customercode = paraMap.get("qry_customercode").toString();
			CpCustomerCompanyEntity customer = this.service.querybyCode(customercode);
			List<Map<String, Object>> billInfos = this.service.queryMapByWhere(" customercode='" + customercode + "'", "queryBillInfoMapByWhere");
			for(Map<String, Object> infoItem : billInfos){
				infoItem.put("customercompanyaddress", ConvertUtil.convertToString(infoItem.get("addressprovince"))
						+ ConvertUtil.convertToString(infoItem.get("addresscity"))
						+ ConvertUtil.convertToString(infoItem.get("addressdistrict"))
						+ ConvertUtil.convertToString(infoItem.get("address")));
			}
			data.put("billlist", billInfos);
			return ResponseResult.ok(data);
		}else{
			return ResponseResult.error("参数中需包含qry_customercode");
		}

	}
}

